﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System;
using System.Collections.Generic;

// 形状接口
public interface IShape
{
    double CalculateArea(); // 计算面积
    bool IsValid(); // 判断形状是否合法
}

// 长方形类
public class Rectangle : IShape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public double CalculateArea()
    {
        return Length * Width;
    }

    public bool IsValid()
    {
        return Length > 0 && Width > 0;
    }
}

// 正方形类
public class Square : IShape
{
    public double SideLength { get; set; }

    public Square(double sideLength)
    {
        SideLength = sideLength;
    }

    public double CalculateArea()
    {
        return SideLength * SideLength;
    }

    public bool IsValid()
    {
        return SideLength > 0;
    }
}

// 三角形类
public class Triangle : IShape
{
    public double BaseLength { get; set; }
    public double Height { get; set; }

    public Triangle(double baseLength, double height)
    {
        BaseLength = baseLength;
        Height = height;
    }

    public double CalculateArea()
    {
        return 0.5 * BaseLength * Height;
    }

    public bool IsValid()
    {
        return BaseLength > 0 && Height > 0;
    }
}

// 简单工厂类
public class ShapeFactory
{
    public static IShape CreateShape(string shapeType, params double[] parameters)
    {
        switch (shapeType.ToLower())
        {
            case "rectangle":
                if (parameters.Length == 2)
                    return new Rectangle(parameters[0], parameters[1]);
                break;
            case "square":
                if (parameters.Length == 1)
                    return new Square(parameters[0]);
                break;
            case "triangle":
                if (parameters.Length == 2)
                    return new Triangle(parameters[0], parameters[1]);
                break;
        }
        return null;
    }
}

class ConsoleApp3
{
    static void Main()
    {
        List<IShape> shapes = new List<IShape>();
        Random random = new Random();

        // 随机创建10个形状对象
        for (int i = 0; i < 10; i++)
        {
            int choice = random.Next(1, 4);
            switch (choice)
            {
                case 1:
                    shapes.Add(ShapeFactory.CreateShape("rectangle", random.Next(1, 10), random.Next(1, 10)));
                    break;
                case 2:
                    shapes.Add(ShapeFactory.CreateShape("square", random.Next(1, 10)));
                    break;
                case 3:
                    shapes.Add(ShapeFactory.CreateShape("triangle", random.Next(1, 10), random.Next(1, 10)));
                    break;
            }
        }

        double totalArea = 0;
        // 计算面积之和
        foreach (var shape in shapes)
        {
            if (shape.IsValid())
            {
                totalArea += shape.CalculateArea();
            }
        }

        Console.WriteLine("所有形状的面积之和为：" + totalArea);
    }
}
